// 轮播图start
layui.use(['carousel', 'form', 'jquery', 'element'], function() {
  var carousel = layui.carousel
    , form = layui.form
    ,element = layui.element
    , $ = layui.$;

  // 个人信息展示
  $.ajax({
    type: 'GET',
    url: '/user/detail/',
    headers: {
      'Authorization': localStorage.getItem('Authorization')
    },
    success: function (msg) {
      // console.log(msg);
      let code = msg.code;
      if (code != 0) {//非登录状态
        $('#user-info').html(
          '<li class="layui-nav-item login">\n' +
          '                    <a href="/static/login.html">亲，请登录</a>\n' +
          '                 </li>\n' +
          '                 <li class="layui-nav-item register">\n' +
          '                    <a href="/static/register.html">免费注册</a>\n' +
          '                 </li>'
        )
      } else {//登录状态
        let nickname = msg.data.email; //没有昵称显示邮箱号
        let detail = msg.data.detail
        if (detail['nickname']) {
          nickname = detail['nickname'];
        }
        $('#nickname').html(nickname);
        $('#portrait_t').attr('src', detail['portrait']);
      }
    }
  });

  //图片轮播
  carousel.render({
    elem: '#test10'
    , width: '1200px'
    , height: '100%'
    , interval: 3000
  });
  // 商品菜单展示
  $.get('/commodity/menu/get_menu/',function (data) {
    let menus = data.data
    let menudata = `` //模板拼接
    // console.log(menus)
    for (let i=0; i<menus.length; i += 3){
      menudata += `<li class="layui-menu-item-parent" lay-options="{type: 'parent'}">
        <div class="layui-menu-body-title">
                        <span class="layui-breadcrumb">`
      for(let x=i;x<i+3;x++){
        if (x<menus.length){
          // console.log(menus[x].fields)
          menudata += `<a href="/static/search.html?id=${menus[x].fields.id}">${menus[x].fields.name}</a>`
        }
      }
      menudata += `</span>
                      </div>
                      <div class="layui-panel layui-menu-body-panel">
                        <ul>`
      for(let y=i;y<i+3;y++){
        if (y<menus.length){
          menudata += `<li lay-options="{id: 1051}">
                            <div class="layui-menu-body-title">
                                <h3><a href="">${menus[y].fields.name}</a></h3>
                                <span class="layui-breadcrumb">`
          if (menus[y].son){
            $.each(menus[y].son,function(i,item){
              menudata += `<a href="/static/search.html?id=${item.id}">${item.name}</a>`
            })
          }
          menudata += `</span>
                            </div>
                          </li>`
        }
      }
      menudata += `</ul>
                      </div>
                    </li>`
      // console.log(menudata);
      $('#menu').html(menudata);
      element.render('breadcrumb');
    }
  });

  // 获取商品数据，展示
  $.get('/commodity/column/',function (data) {
    let columnData = ``
    $.each(data.data,function (i,column) {
      if(column.commodity_list.length>0){
        columnData += `<div class="layui-fluid">
                  <fieldset class="layui-elem-field layui-field-title">
                    <legend><span class="good-title">${column.name}</span>
                        <span class="layui-badge layui-bg-blue">全民口碑</span></legend>
                  </fieldset>
                  <div class="layui-row">`
      $.each(column.commodity_list,function (i,commodity) {
        columnData += `<div class="layui-col-sm3">
                      <div class="grid-demo grid-demo-bg1">
                          <div class="layui-card"><a href="/static/detail.html?id=${commodity.id}">
                              <div class="layui-card-header">
                                  <img src="${commodity.pic}" alt="">
                              </div>
                              <div class="layui-card-body">
                                  <a href="">${commodity.title}</a>
                                  <p>
                                      ${commodity.subtitle}
                                  </p>
                              </div>
                          </a></div>
                      </div>
                    </div>`
      });
      columnData += `</div>
                </div>`
      }
    });
    $('#column').html(columnData);
  });

  // 联想词搜索展示
  $('#inputKeywords').keyup(function () {
    if($('#inputKeywords').val()){
      $.get('/commodity/menu/search/', {"search":$('#inputKeywords').val()},function (data) {
        let menu_list = data.data
        if(menu_list){
          content = ``
          $.each(menu_list,function(i,menu){
            content += `<li><a href="/static/search.html?search=${menu._source.name}">${menu.highlight.name[0]}</a></li>`
          });
          $('.search_item').html(content);
          // $('.search_item').removeClass('layui-hide');
        }
      });
    }else{
      $('.search_item').html(''); // 清空展示
      // $('.search_item').addClass('layui-hide');
    }
  });

  // 实现搜索事件
  function search() {
    window.open(`/static/search.html?search=${$("#inputKeywords").val()}`)
  }
  // 点击搜索
  $('.btn').click(search);
  // 回车搜索
  $('#inputKeywords').keydown(function (event) {
    if(event.keyCode == 13){
      search();
    }
  });

})